class Solution {
public:
    ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) {
        int sizeA = 0;
        int sizeB = 0;
        ListNode* head1 = headA;
        ListNode* head2 = headB;
        while (head1)
        {
            head1 = head1->next;
            sizeA++;
        }
        while (head2)
        {
            head2 = head2->next;
            sizeB++;
        }
        head1 = headA;
        head2 = headB;
        while (sizeA > sizeB)
        {
            head1 = head1->next;
            sizeA--;
        }
        while (sizeA < sizeB)
        {
            head2 = head2->next;
            sizeB--;
        }
        while (head1 != head2)
        {
            head1 = head1->next;
            head2 = head2->next;
        }
        return head1;
    }
};